#include "StdAfx.h"
#include "GameMenu.h"

CGameMenu::CGameMenu(void)
{
}

CGameMenu::~CGameMenu(void)
{
	if(this->_hbmpBackground)
	{
		DeleteObject(this->_hbmpBackground);
	}
	if(this->_hbmpCursor)
	{
		DeleteObject(this->_hbmpCursor);
	}
}

void CGameMenu::Initialize(CGraphics *pGraphics, HINSTANCE hInstance)
{
	this->_nIndex = 0;
	this->_vMenuItems.clear();
	this->_hbmpBackground = pGraphics->LoadBitmapFromResource(IDB_MENUBACK, hInstance);
	this->_hbmpCursor = pGraphics->LoadBitmapFromResource(IDB_CURSOR, hInstance);
	this->_bDifficultySelected = false;
}

bool CGameMenu::DifficultySelected(void)
{
	return _bDifficultySelected;
}

int CGameMenu::GetIndex(void)
{
	return _nIndex;
}

void CGameMenu::Draw(RECT *pClientRect, CGraphics *pGraphics)
{
	if(_vMenuItems.size() > 0)
	{

		pGraphics->ClearPortionOfScreen(BLACK_BRUSH, *pClientRect);
		// first draw the background
		pGraphics->DisplayBitmap(this->_hbmpBackground, (pClientRect->right / 2) - (MENUBACK_WIDTH / 2), (pClientRect->bottom / 2) - (MENUBACK_HEIGHT / 2));

		RECT r;
		r.left = (pClientRect->right / 2);
		r.right = r.left * 2;
		r.top = (pClientRect->bottom / 2) - (MENUBACK_HEIGHT / 2);
		r.bottom = r.top * 2;
		int i;
		for(i = 0; i < (int)_vMenuItems.size(); i++)
		{
			
			pGraphics->DisplayText(_vMenuItems[i], &r, 0);
			r.top += 16;
		}
		int y = (pClientRect->bottom / 2) - (MENUBACK_HEIGHT / 2);
		y += this->_nIndex * 16;
		int x = (pClientRect->right / 2);
		x -= CURSOR_WIDTH;
		
		pGraphics->DisplayTransparentBitmap(this->_hbmpCursor, x, y, RGB(0,0,0));


	}


}

void CGameMenu::HandleKeyStroke(WPARAM wParam)
{
	if(_vMenuItems.size() > 0)
	{
		switch(wParam)
		{
		case VK_UP:
			if(_nIndex == 0)
			{
				_nIndex = (int)_vMenuItems.size() - 1;
			}
			else
			{
				_nIndex--;
			}
			break;
		case VK_DOWN:
			if(_nIndex == (int)_vMenuItems.size() - 1)
			{
				_nIndex = 0;
			}
			else
			{
				_nIndex++;
			}
			break;
		case VK_RETURN:
			this->_bDifficultySelected = true;
			break;

		}
	}
}

void CGameMenu::AddMenuItem(LPCWSTR szMenuItem)
{
	_vMenuItems.push_back(szMenuItem);
}